<template>
<el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="50%"
    @close="handleClose"
    
  >
    <el-form  ref="formRef" :model="form" label-width="100px" :rules="rules">
    <el-form-item label="会议室ID" prop="meetingroomId">
      <el-input v-model="form.meetingroomId"></el-input>
    </el-form-item>
    <el-form-item label="会议室容量" prop="meetingroomSize">
      <el-input v-model="form.meetingroomSize"></el-input>
    </el-form-item>
    <el-form-item label="会议室信息" prop="meetingroomInfo">
      <el-input v-model="form.meetingroomInfo"></el-input>
    </el-form-item>
    
  </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        
        <el-button type="primary" @click="handleConfirm">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>



<script setup>
import {defineEmits,ref,defineProps,watch} from 'vue';
import {$roomAdd, $roomUpdate} from '@/api/roomapi'
import { ElMessage } from 'element-plus'
const props = defineProps({
  dialogTitle:{
    type:String,
    default:'',
    required:true
  },
  dialogTableValue:{
    type:Object,
    default:()=>{}
  }
})
const formRef=ref(null)

const form=ref({
    meetingroomId:'',
    meetingroomSize:'',
    meetingroomInfo:''
})
watch (
  ()=>props.dialogTableValue,
  ()=>{
    
    form.value=props.dialogTableValue
  },
  {deep:true,immediate:true})

const emits =defineEmits(['update:model-value'],'initRoomList')
const handleClose=()=>{
    emits('update:model-value',false)
}

const handleConfirm= ()=>{
    console.log(form.value,"dialog")
    
    formRef.value.validate(async(valid)=>{
      if(valid){
        if(props.dialogTitle==='添加会议室'){
          console.log(form.value,"dialog")
          const data=await $roomAdd(form.value)
          console.log(data.data,"dialog")
          if(data.data=="新建会议室成功"){
            ElMessage({
              message: '添加成功',
              type: 'success',
            })
          }else{
            ElMessage({
              message: '会议室ID已存在',
              type: 'error',
            })
          }
        }else{
          alert("修改会议室")
          const data=await $roomUpdate(form.value)
          console.log(data.data,"dialog")
          if(data.data=='修改会议室成功'){
            ElMessage({
              message: '修改成功',
              type: 'success',
            })
          }else{
            ElMessage({
              message: '修改失败',
              type: 'error',
            })
          }
        }
        
        emits('initRoomList')
        handleClose()
      }else{
        alert("校验格式出现问题")
        return false
      }
    })   
    
}



const rules=ref({
    meetingroomId:[
        {
            required:true,
            message:'请输入会议室id',
            trigger:'blur'
        }
    ],
    meetingroomSize:[
        {
            required:true,
            message:'请输入会议室容量',
            trigger:'blur'
        },
        { type : "string" , required: true , pattern : /^[0-9]+$/ ,message:'注意会议室容量为数字'}
        
    ],
    meetingroomInfo:[
        {
            required:true,
            message:'请输入会议室信息',
            trigger:'blur'
        }
    ]
    
})
</script>

<style lang="scss" scoped>

</style>